<?php

namespace App\Admin\Controllers;

use App\Models\Order;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;

class OrderController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Order(), function (Grid $grid) {
            $grid->model()->with(['user']);
            $grid->model()->with(['recharge_package']);
            $grid->model()->where('status', 1);
            $grid->model()->orderByDesc('id');
            $grid->disableCreateButton();
            $grid->disableRowSelector();
            $grid->disableActions();
            $grid->quickSearch('no', 'recharge_no', 'payment_no', 'cellphone');

            $grid->selector(function (Grid\Tools\Selector $selector) {

                $selector->select('recharge_status', '充值状态', [
                    2 => '成功',
                    4 => '失败'
                ]);

                $selector->select('is_pay', '支付状态', [
                    1 => '已支付',
                    0 => '未支付'
                ]);

                $selector->select('refund_status', '退款状态', [
                    1 => '已退款',
                    0 => '未退款'
                ]);
            });

            $grid->id->sortable();
            $grid->no->copyable();
            $grid->column('user.name');
            $grid->total_amount->display(function () {
                    return finance_format($this->total_amount).'泰铢';
            });
            $grid->cellphone;
            $grid->exchange_rate;
            $grid->real_amount->display(function () {
                    return $this->real_amount/100 .'元';
            });
            $grid->column('recharge_package.title');
            $grid->is_pay->display(function () {
                if($this->is_pay == 0) {
                    return '未支付';
                } else {
                    return '已支付';
                }
            });
            $grid->recharge_status->using([2 => '成功', 4 => '失败'])->badge([
                2 => 'success',
                4 => 'danger',
            ]);
            $grid->payment_no
                ->display('详情') // 设置按钮名称
                ->modal(function ($modal) {
                    $modal->title($this->no . '订单详情');
                    if($this->is_aop == 0) {
                        $aop_type =  '话费充值';
                    } else {
                        $aop_type =  '套餐充值(AOP)';
                    }

                    if($this->recharge_type == 'mtopup') {
                        $recharge_type =  '预付费(mtopup)';
                    } else {
                        $recharge_type = '后付费(billpay)';
                    }

                    if($this->payment_method == 'wechat_pay') {
                        $payment_method = '微信支付';
                    } elseif($this->payment_method == 'ali_pay') {
                        $payment_method = '支付宝';
                    } elseif($this->payment_method === null) {
                        $payment_method = '未知';
                    }

                    if($this->recharge_extra == 'null') {
                        $recharge_extra = '号码：' . $this->cellphone . '充值成功（返回null）';
                    } else {
                        $recharge_extra = $this->recharge_extra;
                    }

                    return  '充值接口详情：' . $this->payment_no .
                            '<br/><br/>充值接口订单号：' . $this->recharge_no .
                            '<br/><br/>支付方式：' . $payment_method .
                            '<br/><br/>订单支付时间：' . $this->paid_at.
                            '<br/><br/>订单类型：' . $aop_type.
                            '<br/><br/>卡类型：' . $recharge_type.
                            '<br/><br/>使用积分：' . $this->credit_num.
                            '<br/><br/>备注：' . $recharge_extra . '-' . $this->remark;
                });
            $grid->refund_status
                ->display('详情') // 设置按钮名称
                ->modal(function ($modal) {
                    $modal->title($this->no . '退款详情');
                    if($this->refund_status == 1) {
                        $refund_status = '已退款';
                        return '退款单号：' . $this->refund_no . '<br/><br/>退款状态：' . $refund_status;
                    } else {
                        $refund_status = '未退款';
                        return '退款状态：' . $refund_status;
                    }
                });

            $grid->created_at;
            $grid->updated_at->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Order(), function (Show $show) {
            $show->id;
            $show->no;
            $show->user_id;
            $show->address;
            $show->total_amount;
            $show->cellphone;
            $show->exchange_rate;
            $show->real_amount;
            $show->recharge_no;
            $show->recharge_type;
            $show->recharge_package_id;
            $show->status;
            $show->is_aop;
            $show->credit_num;
            $show->remark;
            $show->paid_at;
            $show->is_pay;
            $show->payment_method;
            $show->payment_no;
            $show->refund_status;
            $show->refund_no;
            $show->recharge_status;
            $show->recharge_extra;
            $show->closed;
            $show->reviewed;
            $show->ship_status;
            $show->ship_data;
            $show->extra;
            $show->created_at;
            $show->updated_at;
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Order(), function (Form $form) {
            $form->display('id');
            $form->text('no');
            $form->text('user_id');
            $form->text('address');
            $form->text('total_amount');
            $form->text('cellphone');
            $form->text('exchange_rate');
            $form->text('real_amount');
            $form->text('recharge_no');
            $form->text('recharge_type');
            $form->text('recharge_package_id');
            $form->text('status');
            $form->text('is_aop');
            $form->text('credit_num');
            $form->text('remark');
            $form->text('paid_at');
            $form->text('is_pay');
            $form->text('payment_method');
            $form->text('payment_no');
            $form->text('refund_status');
            $form->text('refund_no');
            $form->text('recharge_status');
            $form->text('recharge_extra');
            $form->text('closed');
            $form->text('reviewed');
            $form->text('ship_status');
            $form->text('ship_data');
            $form->text('extra');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
